home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qlib205.zip / QLIB.ZIP / C / QSETUP.C < prev    next >
C/C++ Source or Header  |  1997-07-09  |  4KB  |  175 lines

  1. /*/
  2.  
  3.   QLIB Setup Utility v1.01a
  4.  
  5.   Used to modify MAX/MIN RAM alloc during QLIB startup.
  6.  
  7.   v1.01  - Fixed LE offset determination
  8.   v1.01a - use %u instead of %i to print numbers (prevents printing neg nums)
  9.  
  10. /*/
  11.  
  12. #include <qlib.h>
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include <process.h>
  16. #include <string.h>
  17.  
  18. //#define debug
  19.  
  20. void usage(void) {
  21.   printf("\nUsage : QSETUP [options] <filename[.exe]>\n\n");
  22.   printf("Options:\n");
  23.   printf("--------\n");
  24.   printf("/X<n>   Maximum extended memory to alloc (in bytes)\n");
  25.   printf("        Default = 4 GBs\n");
  26.   printf("/M<n>   Minimum extended memory to alloc (in bytes)\n");
  27.   printf("        Default = 32 KBs\n");
  28.   exit(0);
  29. }
  30.  
  31. void error(byte *s) {
  32.   printf("Error : %s\n",s);
  33.   exit(0);
  34. }
  35.  
  36. int h;
  37. #define bufsiz 64*1024
  38. byte buf[bufsiz];
  39. byte bufc;
  40. dword max;
  41. dword min;
  42. dword omax;
  43. dword omin;
  44. byte domax=0;
  45. byte domin=0;
  46. byte ts[0x80];
  47. byte fn[0x80];
  48. dword bufs;
  49. dword offset;
  50. dword bufp;
  51.  
  52. struct {
  53.   word sign;
  54.   word siz1;
  55.   word siz2;
  56.   word reits;
  57.   word headsiz;
  58. } EXE;
  59.  
  60. //total size of EXE part = (headsiz*16)+(siz2-1)*512+siz1
  61.  
  62. void qmax(byte * s) {
  63.   if (!memicmp(s,"0x",2)) {
  64.     max=str2numx(s);
  65.   } else {
  66.     max=str2num(s);
  67.   }
  68.   domax=1;
  69. }
  70.  
  71. void qmin(byte * s) {
  72.   if (!memicmp(s,"0x",2)) {
  73.     min=str2numx(s);
  74.   } else {
  75.     min=str2num(s);
  76.   }
  77.   domin=1;
  78. }
  79.  
  80. union {
  81.   dword d1 ;
  82.   struct {
  83.     byte b1;
  84.     byte b2;
  85.     byte b3;
  86.     byte b4;
  87.   } b;
  88. } conv;
  89.  
  90. void putmm(dword off,dword m) {
  91.   conv.d1=m;
  92.   buf[off+0]=conv.b.b1;
  93.   buf[off+1]=conv.b.b2;
  94.   buf[off+2]=conv.b.b3;
  95.   buf[off+3]=conv.b.b4;
  96. }  
  97.  
  98. //not needed anymore
  99. dword getmm(dword off) {
  100.   conv.b.b1=buf[off+0];
  101.   conv.b.b2=buf[off+1];
  102.   conv.b.b3=buf[off+2];
  103.   conv.b.b4=buf[off+3];
  104.   return conv.d1;
  105. }  
  106.  
  107. void main(byte argc,byte **args) {
  108.   word a;
  109.   word t1;
  110.   printf("\nQLIB Setup Utility   v1.01a  (by : Peter Quiring)\n");
  111.   if (argc<2) usage();
  112.   fn[0]=0;
  113.   for (a=1;a<argc;a++) {
  114.     if (! memicmp(args[a],"/x",2)) {qmax(args[a]+2);continue;}
  115.     if (! memicmp(args[a],"/m",2)) {qmin(args[a]+2);continue;}
  116.     if (fn[0]) usage();
  117.     strcpy(fn,args[a]);
  118.   }
  119.   if (!fn[0]) usage();
  120.   if ((! domax) && (! domin)) usage();
  121.   h=open(fn,O_BINARY|O_RDWR);
  122.   if (h==-1) {
  123.     strcat(fn,".EXE");
  124.     h=open(fn,O_BINARY|O_RDWR);
  125.     if (h==-1) error("Unable to find file");
  126.   }
  127.   printf("\nWorking with : %s\n",fn);
  128.   if (read(h,&EXE,sizeof(EXE))!=sizeof(EXE))
  129.     error("Unable to read EXE header");
  130.   offset=(EXE.siz2-1)*512;   //(EXE.headsiz*16)+(EXE.siz2-1)*512+EXE.siz1;
  131.   t1=(EXE.siz1 + 0xf) & 0xfff0;
  132.   offset+=t1;
  133.   lseek(h,offset,SEEK_SET);
  134. #ifdef debug
  135.   printf("Offset = 0x%x\n",offset);
  136. #endif
  137.   bufs=read(h,buf,bufsiz);
  138.   if ((!bufs) || (memcmp(buf,"LE",2)))
  139.     error("Unable to read LE header (Compressed?)");
  140.   bufp=0;
  141.   while (1) {  //find offset of 'QLIB_MEM'
  142.     if (! memcmp(buf+bufp,"QLIB_MEM",8)) break;
  143.     bufp++;offset++;
  144.     if (bufp>bufs) {
  145.       bufs=read(h,buf,bufsiz);
  146.       if (!bufs) error("This is not a QLIB EXE file!");
  147.       bufp=0;
  148.     }
  149.   }
  150.   //patch the EXE as needed
  151.   lseek(h,offset,SEEK_SET);
  152.   read(h,buf,16);
  153.   if (memcmp(buf,"QLIB_MEM",8)) error("This is not a QLIB EXE file!");
  154.   if (!domax) max=0xffffffff;  //like there will ever be EXACTLY 4 gigs free
  155.   if (!domin) {
  156.     if (max<32*1024) min=max; else min=32*1024;
  157.   }
  158.  
  159.   if (max<min) error("Max < Min ?");
  160.  
  161.   putmm(8,max);
  162.   printf("Maximum extended memory set to : %u\n",max);
  163.   putmm(12,min);
  164.   printf("Minimum extended memory set to : %u\n",min);
  165.  
  166. #ifdef debug
  167.   printf("Offset(QLIB) = 0x%x\n",offset);
  168. #endif
  169.   lseek(h,offset,SEEK_SET);
  170.   write(h,buf,16);
  171.   close(h);
  172.   printf("\nComplete!\n");
  173. }
  174.  
  175.